
getbetasagr=function(object,row,column)
{
  if (any(class(object)=="eiMD")){
    Beta=object$draws$Beta
    if (is.null(Beta))
      stop("blablabla")
  }
  else if (any(class(object)=="eiMD.beta")){
    Beta=object
  }
  else stop("blablablabla")
  if (is.mcmc(Beta)){
    tnames=strsplit(colnames(Beta),"beta.")
    get2=function(x) x[2]
    idx=strsplit(sapply(tnames,get2),".",fixed=TRUE)
    idx=as.list(as.data.frame(matrix(unlist(idx),byrow=TRUE,
                                     nrow=length(idx),ncol=length(idx[[1]]))))
    idx=lapply(idx,as.character)
    idx=lapply(idx,unique)
    idx[[4]]=1:length(Beta[,1])
    names(idx)=c("rows","columns","precints","sims")
    Betas=array(t(object$draws$Beta),dim=sapply(idx,length),dimnames=idx)
  }
  else {
    idx=dimnames(Beta)
  }
  if (missing(row) | missing(column)) {
    stop("blablabla")
  }
  frow=idx$rows[grep(row,idx$rows)]
  fcolumn=idx$columns[grep(column,idx$columns)]
  if (length(frow)<1)
    stop("blablabla")
  if (length(frow)>2)
    stop("blablabla")
  if (length(fcolumn)<1)
    stop("blablabla")
  if (length(fcolumn)>2)
    stop("blablabla")
  usebetas=t(Betas[frow,fcolumn, ,])
  quant.low=function(x) {
    quantile(x,c(0.5-0.95/2))
  }   
  quant.high=function(x) {
    quantile(x,c(0.5+0.95/2))
  }       
  tbetas=t(usebetas)
  aggregatebetas=apply(tbetas,2,mean)
  agregmean=mean(aggregatebetas)
  agregsd=sd(aggregatebetas)
  agreg025=quantile(aggregatebetas,0.025)
  agreg975=quantile(aggregatebetas,0.975)
  output=data.frame(agregmean,agregsd,agreg025,agreg975)
}
